package com.ilvyou.api.util;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.ilvyou.core.util.StringUtil;
import lombok.Data;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLEncoder;

/**
 * Created by GuanYuCai on 2016/9/7 0007.
 */
public class RemoteUtil {

    public static String getMd5Pwd(String pwd, String salt) throws IOException {

        String url = "http://www.ilvzan.com/Account/GetPwdSalt?pwd=" + URLEncoder.encode(pwd, "UTF-8") + "&salt=" + salt;
        String json = loadJSON(url);

        String md5Pwd = "0";
        if (!StringUtil.isEmpty(json)){
            ObjectMapper mapper = new ObjectMapper();
            JsonNode node = mapper.readTree(json);
            md5Pwd = node.get("pwd").textValue();
        }
        return md5Pwd;
    }

    public static String sendSMS(String mobile, String msg) throws Exception {
        Message p = new Message("LHFAPP", mobile, msg, "10.8.18.48", "0", "0");
        String pStr = new ObjectMapper().writeValueAsString(p);
        URL url = new URL("http://dx.lyapi.cn/sendonemsg/message");
        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
        conn.setRequestMethod("POST");
        conn.setRequestProperty("Content-Type","application/json");
        conn.setRequestProperty("Connection", "Keep-Alive");

        conn.setUseCaches(false);
        conn.setDoOutput(true);

        OutputStreamWriter out = new OutputStreamWriter(conn.getOutputStream(), "UTF-8");
        out.write(pStr);
        out.flush();
        out.close();

        // 获取响应内容体
        BufferedReader in = new BufferedReader(new InputStreamReader(
                conn.getInputStream(), "UTF-8"));
        String line, result = "";
        while ((line = in.readLine()) != null) {
            result += line;
        }
        in.close();
        return result;
    }

    private static String loadJSON (String url) throws IOException {
        StringBuilder json = new StringBuilder();
        URLConnection yc = new URL(url).openConnection();

        yc.setRequestProperty("accept", "*/*");
        yc.setRequestProperty("connection", "Keep-Alive");
        yc.setRequestProperty("user-agent",
                "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");

        BufferedReader in = new BufferedReader(new InputStreamReader(yc.getInputStream()));
        String inputLine = null;
        while ( (inputLine = in.readLine()) != null) {
            json.append(inputLine);
        }
        in.close();
        return json.toString();
    }

    @Data
    static class Message {
        // 短信请求来自哪个平台：PC/M/APP/BPP/MG等等是平台的字母简写
        private String platform ;

        // 接收的短信号码
        private String mobilenum ;

        // 发送的短信内容
        private String msg;

        // 用户本地的IP地址
        private String localip;

        // 用户本地的经度
        private String longitude;

        // 用户本地的纬度
        private String latitude;

        public Message(String platform, String mobilenum, String msg,
                       String localip, String longitude, String latitude) {
            this.platform = platform;
            this.mobilenum = mobilenum;
            this.msg = msg;
            this.localip = localip;
            this.longitude = longitude;
            this.latitude = latitude;
        }
    }
}
